package com.test.framework.context.support;

import com.test.framework.beans.BeanDefinition;
import com.test.framework.beans.factory.support.BeanDefinitionReader;
import com.test.framework.beans.factory.support.BeanDefinitionRegistry;
import com.test.framework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: AbstractApplicationContext
 * @description: ApplicationContext接口的子实现类,用于立即加载
 * @author: Mr du
 * @create: 2024-04-10 21:05
 */
public abstract class AbstractApplicationContext implements ApplicationContext {

    //声明解析器
    protected BeanDefinitionReader beanDefinitionReader;

    //用来存储bean对象的容器   key存储的是bean的id值，value存储的是bean对象
    protected Map<String,Object> singletonObjects = new HashMap<>();

    //声明存储配置文件的路径
    protected String configLocation;

    @Override
    public void refresh() throws IllegalStateException, Exception {
        //加载BeanDefinition
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        this.finishBeanInitialization();
    }

    //初始化Bean
    private void finishBeanInitialization() throws Exception{
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        //获取所有的bean Name
        String[] beanNames = registry.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
            getBean(beanName);
        }
    }
}
